package com.kylexy.closes.ui.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.kylexy.closes.R;
import com.kylexy.closes.bean.CloseInfo;
import com.tl.common.util.StringUtils;

public class CloseInfoAdapter extends BaseAdapter {
	List<CloseInfo> mDatas = new ArrayList<CloseInfo>();
	LayoutInflater mInflater;
	int mTextRow = 1; // 文字自动换行造成最后一列显示不全
	Context mContext;

	public CloseInfoAdapter(Context ctx) {
		mInflater = LayoutInflater.from(ctx);
		mContext = ctx;
	}

	public void setDatas(List<CloseInfo> datas) {
		mDatas = datas;
	}

	@Override
	public int getCount() {
		return mDatas.size();

	}

	@Override
	public CloseInfo getItem(int position) {

		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_closeinfo,
					parent, false);
		}

		CloseInfo data = getItem(position);

		ImageView iv = (ImageView) convertView
				.findViewById(R.id.item_closeinfo_pic);
		TextView tv = (TextView) convertView
				.findViewById(R.id.item_closeinfo_text);
		tv.setText(Html.fromHtml(getHtml(data)));

		//默认图片
		iv.setImageResource(R.drawable.default_closes);
		// 设置图片
		String url = data.getClosePic();
		if ((url != null) && !StringUtils.isEmpty(url)) {
			File file = new File(url);
			if (file.exists()) {
				AQuery aq = new AQuery(convertView);
				aq.id(R.id.item_closeinfo_pic).image(file, 100);
			}

		}

		return convertView;
	}

	private String getHtml(CloseInfo data) {

		StringBuilder sb = new StringBuilder();
		sb.append("<font color=\"black\">条码： </font>");

		sb.append("<font color=\"blue\">" + data.getCloseId() + "</font>");

		sb.append("<br />");
		sb.append("<font color=\"black\">商品： </font>");

		sb.append("<font color=\"blue\">" + data.getCloseName() + "</font>");

		return sb.toString();
	}
}